package com.viboo.concern.service.impl;

import com.viboo.concern.dao.RankMapper;
import com.viboo.concern.entity.Rank;
import com.viboo.concern.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author avanline
 * @descriotion
 * @date 2018-02-28 19:56
 */
@Service("rankService")
public class RankServiceImpl implements RankService {

    @Autowired
    RankMapper rankMapper;

    @Override
    public boolean addRank(Rank rank) throws Exception {
        Rank mRank = rankMapper.selectRank(rank);
        if (mRank != null) {//判断是否存过该用户访问信息
            rank.setEntryNub(mRank.getEntryNub() + 1);//有添加一次访问记录
            //修改值
            return updateEntryNub(rank) != 0;
        }
        rank.setEntryNub(1);//没有设置第一次访问值为1
        rank.setRanking(count(rank.getUserId()) + 1);
        return rankMapper.insert(rank) != 0;
    }

    @Override
    public Rank selectUser(Rank rank) throws Exception {
        return rankMapper.selectRank(rank);
    }

    @Override
    public List<Rank> selectAllRank(Rank rank) throws Exception {
        return rankMapper.selectAllRank(rank);
    }

    @Override
    public int updateEntryNub(Rank rank) throws Exception {
        return rankMapper.updateEntryNub(rank);
    }

    @Override
    public int count(String userId) throws Exception {
        return rankMapper.count(userId);
    }

    @Override
    public int concernIndex(String userId) throws Exception {
        return rankMapper.concernIndex(userId);
    }

    @Override
    public int deleteByUserId(String userId) {
        return rankMapper.deleteByUserId(userId);
    }
}
